//
//  ViewController.m
//  TechnologyCultural
//
//  Created by 李淋 on 10/18/15.
//  Copyright © 2015 李淋. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *techHolder;

@property (weak, nonatomic) IBOutlet UILabel *keLabel;
@property (weak, nonatomic) IBOutlet UILabel *jiLabel;
@property (weak, nonatomic) IBOutlet UILabel *renLabel;
@property (weak, nonatomic) IBOutlet UILabel *wenLabel;
@property (weak, nonatomic) IBOutlet UIButton *techBtn;

@property (strong, nonatomic) NSTimer *timer;

@end

static BOOL control=YES;
static double angle=10;

typedef enum : NSUInteger {
    IN_CIRCLE = 0,
    ON_CIRCLE = 1,
    OUT_CIRCLE = 2,
} CircleState;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    control = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated {
    
    if (control) {
        [self rotateView:_jiLabel Angle:90];
        [self rotateView:_wenLabel Angle:180];
        [self rotateView:_keLabel Angle:270];
        
        [self startRotate:_techHolder Delay:0];
    }
    
    control = NO;
}

-(void)putTechOnCircle {
    _keLabel.hidden = NO;
    _jiLabel.hidden = NO;
    _techBtn.hidden = YES;
}

- (IBAction)touchMove:(id)sender forEvent:(UIEvent *)event {
    UITouch *touch = [[event touchesForView:sender] anyObject];
    CGPoint movingPoint = [touch locationInView:[sender superview]];
    UIButton *button = (UIButton*) sender;
    button.center = movingPoint;
    if ([self circleStateOfPoint:movingPoint] == ON_CIRCLE) {
        if (!_timer) {
            _timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(putTechOnCircle) userInfo:nil repeats:NO];
        }
    }
    else {
        if (_timer) {
            [_timer invalidate];
            _timer = nil;
        }
    }
}
- (IBAction)touchEnd:(id)sender forEvent:(UIEvent *)event {
    NSLog(@"拖动结束");
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
}


-(void) startRotate:(UIView*)view Delay:(float)delay
{
    
    [UIView animateWithDuration:0.1 delay:delay options:UIViewAnimationOptionCurveLinear animations:^{
        view.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
    } completion:^(BOOL finished) {
        angle += 10;
        [self startRotate:view Delay:0];
    }];
}

-(void)rotateView:(UIView*)view Angle:(CGFloat)angle {
    view.transform = CGAffineTransformMakeRotation(angle*(M_PI / 180.0f));
}

-(CircleState)circleStateOfPoint:(CGPoint)point {
    CGPoint centerOfView = self.view.center;
    CGFloat radius = (_wenLabel.frame.origin.y+_wenLabel.frame.size.height/2 - _renLabel.frame.origin.y - _renLabel.frame.size.height/2)/2;
    CGFloat minRadius = radius - _renLabel.frame.size.height;
    CGFloat maxRadius = radius + _renLabel.frame.size.height;
    
    if ([self calculateDistanceBetweenPointA:centerOfView AndPointB:point] < minRadius) {
        return IN_CIRCLE;
    }
    else if ([self calculateDistanceBetweenPointA:centerOfView AndPointB:point] >= minRadius &&
             [self calculateDistanceBetweenPointA:centerOfView AndPointB:point] <= maxRadius) {
        return ON_CIRCLE;
    }
    else if ([self calculateDistanceBetweenPointA:centerOfView AndPointB:point] > maxRadius) {
        return OUT_CIRCLE;
    }
    
    return IN_CIRCLE;
}

-(CGFloat)calculateDistanceBetweenPointA:(CGPoint)a AndPointB:(CGPoint)b {
    CGFloat distance = sqrt(pow(fabsf(b.y-a.y), 2) + pow(fabsf(b.x-a.x), 2));
    return distance;
}

@end
